package com.pega.rasmus.ydict;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class ydict extends Activity {

	static final String TAG = "rasmus";	// for trace log
	protected static final int GUINOTIFIER = 0xc001;
	
	private Button mButton;
	private EditText mEditText;
	private TextView mTextView;
	private ProgressDialog myDialog = null;
	private String queryResult = "";
	protected Handler mHandler;


	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		// get widgets
		mButton = (Button)findViewById(R.id.myButton);
		mEditText = (EditText)findViewById(R.id.myEditText);
		mTextView = (TextView)findViewById(R.id.myTextView);

		// set click listener to button
		mButton.setOnClickListener( myButtonListener );

		// handler for textview
		mHandler = new Handler()  {
			@Override
			public void handleMessage(Message msg)  {

				switch (msg.what)
				{
				case GUINOTIFIER:
					mTextView.setText(queryResult);
					break;
				}
				super.handleMessage(msg);
			}
		};	// handler

    }

    // button to invoke query
	Button.OnClickListener myButtonListener = new OnClickListener()  {
		public void onClick(View v)  {
			mTextView.setText("");	// clear the textview
			String body = getString(R.string.STR_DLG_PLEASE_WAIT);
			String title = getString(R.string.STR_APP_NAME);
			
			// TODO start a thread to lookup the dictionary
      		myDialog = ProgressDialog.show(ydict.this, title, body, false);
			//Toast.makeText(ydict.this, body, Toast.LENGTH_LONG).show();
			
      		// thread to perform query action
      		new Thread(new Runnable()  {
				public void run()  {
					try  {
						String str = mEditText.getText().toString();
						// to query the ''str'' via ydict
						Log.v(TAG, "send a query " + str + "...");
						queryResult = getLookup(str);
						Log.v(TAG, "send a message to handler");
						/* send message to handler after retrieve the query */
						Message m = new Message();
						m.what = GUINOTIFIER;
						mHandler.sendMessage(m);
					}
					catch (Exception e)  {
						e.printStackTrace();
						Log.e(TAG, "unhandled exceptions");
					}
					finally  {
						myDialog.dismiss();
					}
				}	// run()
			}).start();
		}	// onClick()
	};	// new OnClickListener ends

    @Override
    public boolean onCreateOptionsMenu(Menu menu)  {
        new MenuInflater(getApplication()).inflate(R.menu.mainmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_show:
				new AlertDialog.Builder(this)
				.setTitle(R.string.STR_APP_NAME)
				.setMessage(R.string.STR_DLG_MSG)
				.setPositiveButton(R.string.STR_DLG_OK,
						new DialogInterface.OnClickListener()  {
							public void onClick(
									DialogInterface dialoginterface, int i){
						}	// DialogInterface
						})
				.show();
            	return true;
			case R.id.menu_finish:
				finish();
				break;
		}
		return (super.onOptionsItemSelected(item));
	}

    public String getLookup(String str)  {
    	//CharSequence pre = getString(R.string.STR_PREFIX);
    	//CharSequence post = getString(R.string.STR_POSTFIX);
    	//return (pre + " " + str + " " + post);
    	return (myLookup.Query(str));
    }
    
    public String myGetString(int id)  {
    	return getString(id);
    }

}
